package com.yipao.db.service;

import com.github.pagehelper.PageHelper;
import com.yipao.db.dao.AdminMapper;
import com.yipao.db.domain.YipaoAdmin;
import com.yipao.db.domain.YipaoAdmin.Column;
import com.yipao.db.domain.YipaoAdminExample;
import com.yipao.db.util.DeleteStatusConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 管理员
 *
 * @author tianhao
 */
@Service
public class YipaoAdminService {
    private final Column[] result = new Column[]{Column.id, Column.username, Column.mobile, Column.avatar, Column.roleIds, Column.weixinOpenId};

    @Resource
    private AdminMapper adminMapper;

    public List<YipaoAdmin> findAdmin(String username) {
        YipaoAdminExample example = new YipaoAdminExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return adminMapper.selectByExample(example);
    }

    public YipaoAdmin findAdmin(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public List<YipaoAdmin> querySelective(String username, Integer page, Integer limit, String sort, String order) {
        YipaoAdminExample example = new YipaoAdminExample();
        YipaoAdminExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return adminMapper.selectByExampleSelective(example, result);
    }

    public int updateById(YipaoAdmin admin) {
        admin.setUpdateTime(LocalDateTime.now());
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(YipaoAdmin admin) {
        admin.setAddTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.insertSelective(admin);
    }

    public YipaoAdmin findById(Integer id) {
        return adminMapper.selectByPrimaryKeySelective(id, result);
    }

    public List<YipaoAdmin> all() {
        YipaoAdminExample example = new YipaoAdminExample();
        example.or().andDeletedEqualTo(false);
        return adminMapper.selectByExample(example);
    }

    /**
     * 运维端,根据微信openId查询
     *
     * @param weixinOpenid
     * @return
     */
    public YipaoAdmin queryByWeixinOpenId(String weixinOpenid) {
        return adminMapper.queryByWeixinOpenId(weixinOpenid, DeleteStatusConstant.NOT_DELETED);
    }

    /**
     * 运维端,根据微信手机号码查询
     *
     * @param mobile
     * @return
     */
    public YipaoAdmin findByMobile(String mobile) {
        return adminMapper.queryByMobile(mobile, DeleteStatusConstant.NOT_DELETED);
    }
}
